﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.IO;
using System;
using System.IO;

namespace Blueprints
{
    partial class DeserializationContext<ResultType>
    {
        /// <summary>
        /// Deserializes a blueprint.
        /// </summary>
        /// <param name="reader">The reader from which to read.</param>
        /// <param name="targetType">The type of object to deserialize.</param>
        /// <param name="id">The id of node containing the object to deserialize.</param>
        /// <returns>The deserialized object.</returns>
        public ResultType Deserialize(IBlueprintReader reader, Type targetType = null, string id = null)
        {
            if (targetType == null)
            {
                targetType = typeof(object);
            }

            using (NodeInfoReader nodeReader = new NodeInfoReader(reader))
            {
                if (string.IsNullOrEmpty(id))
                {
                    if (!nodeReader.MoveToStartNode())
                    {
                        throw new BlueprintException("Document has no valid content");
                    }
                }
                else
                {
                    if (!nodeReader.MoveToId(id))
                    {
                        throw new BlueprintException("Document does not contain a node with the requested id '" + id + "'");
                    }
                }

                return Deserialize(nodeReader, targetType);
            }
        }

        /// <summary>
        /// Deserializes a blueprint.
        /// </summary>
        /// <param name="stream">The stream from which to read.</param>
        /// <param name="targetType">The type of object to deserialize.</param>
        /// <param name="uri">The uri at which the blueprint is located. This is used to resolve relative links within the blueprint.</param>
        /// <param name="mediaType">The media type with which the blueprint is stored.</param>
        /// <returns>The deserialized object.</returns>
        public ResultType Deserialize(Stream stream, Type targetType = null, Uri uri = null, MediaType mediaType = MediaType.Unknown)
        {
            if (uri == null)
            {
                uri = new Uri("this:");
            }
            else if (mediaType == MediaType.Unknown)
            {
                mediaType = uri.GetMediaType();
            }

            using (IBlueprintReader reader = BlueprintResolver.CreateReader(stream, uri, mediaType))
            {
                return Deserialize(reader, targetType);
            }
        }

        /// <summary>
        /// Deserializes a blueprint.
        /// </summary>
        /// <param name="uri">The uri at which the blueprint is located.</param>
        /// <param name="targetType">The type of object to deserialize.</param>
        /// <param name="mediaType">The media type with which the blueprint is stored.</param>
        /// <returns>The deserialized object.</returns>
        public ResultType Deserialize(Uri uri, Type targetType = null, MediaType mediaType = MediaType.Unknown)
        {
            MediaType uriMediaType;
            using (Stream stream = BlueprintResolver.CreateReaderStream(uri, out uriMediaType))
            {
                if (mediaType == MediaType.Unknown)
                {
                    mediaType = uriMediaType;
                }

                using (IBlueprintReader reader = BlueprintResolver.CreateReader(stream, uri, mediaType))
                {
                    return Deserialize(reader, targetType, uri.GetFragment());
                }
            }
        }
    }
}